<template>
	<view>
		<view class="main">
			<view class="tit"><text></text>优惠券名</view>
			<input v-model="coupon_name" placeholder="活动任务名" />
			<view class="tit"><text></text>类型</view>
			<view class="checkbox">
				<uni-data-checkbox v-model="type" :localdata="range1"></uni-data-checkbox>
			</view>
			<view class="tit"><text></text>减金额/折扣</view>
			<input v-model="price" placeholder="减金额/折扣" type="number" />
			<view class="tips">
				满减券减10元，填10；<br>
				折扣券8折，填8；
			</view>
			<view class="tit"><text></text>满足金额</view>
			<input v-model="reach_price" placeholder="满足金额" type="number" />
			<view class="tit"><text></text>到期类型</view>
			<view class="checkbox">
				<uni-data-checkbox v-model="time_type" :localdata="range2"></uni-data-checkbox>
			</view>
			<view v-if="time_type==1">
				<view class="tit"><text></text>固定时间</view>
				<input v-model="rangeTime" placeholder="点击选择" disabled @click="show = true" />
			</view>
			<view v-if="time_type==2">
				<view class="tit"><text></text>到期天数</view>
				<input v-model="valid_day" placeholder="到期天数" type="number" />
			</view>
			<view class="tit"><text></text>发券数</view>
			<input v-model="limit" placeholder="发券数" type="digit" />
			<view class="tit"><text></text>规则</view>
			<textarea placeholder="优惠券规则说明" v-model="use_rule"></textarea>
			<view class="subBtn" @click="toSubmit()">确定</view>
		</view>
		<u-calendar :show="show" mode="range" @confirm="confirm" @close="show = false"></u-calendar>
	</view>
</template>

<script>
	const d = new Date()
	const year = d.getFullYear()
	let month = d.getMonth() + 1
	month = month < 10 ? `0${month}` : month
	const date = d.getDate()
	import {
		activityUserAdd
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				maxDate: `${year+1}-${month}-${date + 30}`,
				show: false,
				coupon_name: '',
				type: 1,
				price: '0',
				reach_price: '0',
				time_type: 1,
				start_time: '',
				end_time: '',
				rangeTime: '',
				valid_day: 0,
				limit: '',
				use_rule: '',
				range1: [{
					"value": 1,
					"text": "满减券"
				}, {
					"value": 2,
					"text": "折扣券"
				}],
				range2: [{
					"value": 1,
					"text": "固定时间"
				}, {
					"value": 2,
					"text": "领取到期天数"
				}],
				infos:''
			};
		},
		onLoad(option) {
			if(option.item){
				let obj = JSON.parse(option.item)
				this.infos = obj
				console.log(this.infos)
				uni.setNavigationBarTitle({
					title:'编辑'
				})
				this.coupon_name = obj.coupon_name
				this.type = obj.type
				this.price = obj.price
				this.reach_price = obj.reach_price
				this.time_type = obj.time_type
				this.valid_day = obj.valid_day
				this.limit = obj.limit
				this.use_rule = obj.use_rule
				if(obj.start_time!=null){
					this.start_time = obj.start_time
					this.end_time = obj.end_time
					this.rangeTime = obj.start_time + '~' + obj.end_time
				}
			}
		},
		methods: {
			confirm(e) {
				console.log(e)
				let length = e.length
				this.start_time = e[0]
				this.end_time = e[e.length - 1]
				this.rangeTime = e[0] + '~' + e[e.length - 1]
				this.show = false
			},
			async toSubmit() {
				let obj = {
					'row[coupon_name]': this.coupon_name,
					'row[type]': this.type,
					'row[price]': this.price,
					'row[reach_price]': this.reach_price,
					'row[time_type]': this.time_type,
					'row[start_time]': this.start_time,
					'row[end_time]': this.end_time,
					'row[valid_day]': this.valid_day,
					'row[limit]': this.limit,
					'row[use_rule]': this.use_rule,
				}
				if(this.time_type==2){
					obj = {
						'row[coupon_name]': this.coupon_name,
						'row[type]': this.type,
						'row[price]': this.price,
						'row[reach_price]': this.reach_price,
						'row[time_type]': this.time_type,
						'row[valid_day]': this.valid_day,
						'row[limit]': this.limit,
						'row[use_rule]': this.use_rule,
					}
				}else{
					obj = {
						'row[coupon_name]': this.coupon_name,
						'row[type]': this.type,
						'row[price]': this.price,
						'row[reach_price]': this.reach_price,
						'row[time_type]': this.time_type,
						'row[start_time]': this.start_time,
						'row[end_time]': this.end_time,
						'row[limit]': this.limit,
						'row[use_rule]': this.use_rule,
					}
				}
				if (this.infos != '') {
					let that = this
					uni.request({
						url: 'https://jzxt.jiugezixun.cn/storeadmin/business_coupon/edit/ids/' + that.infos.id,
						data: obj,
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'X-Requested-With': 'XMLHttpRequest',
							'Cookie': uni.getStorageSync('cookies') // 从存储中读取cookies并设置到请求头
						},
						success: result => {
							console.log(result)
							if (result.data.code == 1) {
								uni.showToast({
									title: '修改成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 500)
							} else {
								uni.showToast({
									title: result.data.msg,
									icon: 'none'
								})
							}
						},
						fail: result => {
							uni.hideLoading();
						}
					})
				}else{
					const result = await activityUserAdd(obj)
					if (result.code == 1) {
						uni.showToast({
							title: '添加成功'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					} else {
						uni.showToast({
							title: result.msg,
							icon: 'error'
						})
					}
				}
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	.top-img {
		width: 100%;
		height: 422rpx;
	}

	.main {
		margin-top: 15rpx;
		background: white;
		padding: 7rpx 30rpx;

		.title {
			font-size: 32rpx;
			font-weight: bold;
		}

		.tit {
			font-size: 28rpx;
			margin: 17rpx 0;
			font-weight: bold;

			text {
				color: #FF0000;
			}
		}

		input {
			padding: 14rpx 15rpx;
			font-size: 20rpx;
			border: solid 1rpx #707070;
			border-radius: 10rpx;
		}

		.btns {
			margin: 20rpx 40rpx;

			view {
				width: 250rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 10rpx;
				justify-content: center;
				color: white;
				text-align: center;
			}

			.btn-item1 {
				background: #DD4E3D;
			}

			.btn-item2 {
				background: #5D9CEB;
			}

			text {
				margin-left: 10rpx;
			}
		}

		.btns2 {
			margin: 20rpx 0;

			view {
				width: 200rpx;
			}
		}

		.tips {
			font-size: 20rpx;
			margin: 15rpx 0;
		}

		.slider {
			font-size: 20rpx;

			.slider-tips {
				font-size: 20rpx;
				position: absolute;
				bottom: 0;
				left: 30rpx;
			}
		}

		.subBtn {
			font-size: 30rpx;
			color: white;
			padding: 8rpx 95rpx;
			background: #5D9CEB;
			width: fit-content;
			margin: 137rpx auto;
			border-radius: 10rpx;
		}

		textarea {
			border: solid 1rpx #707070;
			padding: 15rpx 9rpx;
			border-radius: 10rpx;
			font-size: 20rpx;
		}

		.change {
			input {
				width: 80%;
			}

			view {
				width: 44rpx;
				height: 44rpx;
				border: solid 8rpx #E0E0E0;
				background: white;
				border-radius: 4rpx;
			}
		}
	}
</style>